package com.tiny.tests.effects;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import com.tiny.core.game.Game;
import com.tiny.core.input.KeyboardManager;
import com.tiny.core.resources.ImageManager;
import com.tiny.effects.Effect;
import com.tiny.effects.FadeInOutEffect;

public class FadeInOutEffectTest extends Game {

    private Effect fadeIn;
    private Effect fadeOut;

    private Effect current;

    private ImageManager imgManager;
    private BufferedImage img;

    private int seconds;

    @Override
    public void onLoad() {
        imgManager = new ImageManager();
        img = imgManager.loadImage("resources/tiny-logo-small.png");

        fadeIn = new FadeInOutEffect(this, true, 2000);
        fadeOut = new FadeInOutEffect(this, false, 2000);

        current = fadeOut;
    }

    @Override
    public void onUnload() {

    }

    @Override
    public void onUpdate(int currentTick) {
        if (current.isFinished()) {
            change();
            current.start();
        }
        current.update(currentTick);
        if (currentTick % getGameConfig().getExpectedTPS() == 0) {
            seconds++;
        }

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_ESCAPE)) {
            terminate();
        }
    }

    private void change() {
        if (current == fadeIn) {
            current = fadeOut;
        } else {
            current = fadeIn;
        }
    }

    @Override
    public void onRender(Graphics2D g) {
        g.setColor(Color.WHITE);
        g.drawString("Time: " + seconds, 500, 20);

        Graphics2D g2d = (Graphics2D)g.create();
        current.render(g2d);
        g2d.drawImage(img, null, 100, 100);

        g.drawString("Working!!!", 300, 300);
    }

    @Override
    public String getTitle() {
        return "FadeInOutEffect";
    }

    public static void main(String[] args) {
        new FadeInOutEffectTest().run();
    }
}
